package com.badu.fx.service.impl;

import com.badu.fx.domain.FxLevelRecord;
import com.badu.app.dao.AppUserMapper;
import com.badu.app.domain.AppUser;
import com.badu.common.exception.BDException;
import com.badu.fx.dao.FxLevelRecordMapper;
import com.badu.fx.service.IFxLevelRecordDefaultService;
import com.badu.fx.service.IFxLevelRecordService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;


import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 分销级别关联表 服务实现类
 * </p>
 *
 * @author astupidcoder123
 * @since 2021-01-13
 */
@Slf4j
@Service
@Transactional
public class FxLevelRecordServiceImpl extends ServiceImpl<FxLevelRecordMapper, FxLevelRecord>
		implements IFxLevelRecordService, IFxLevelRecordDefaultService {

	@Autowired
	private FxLevelRecordMapper fxLevelRecordMapper;
	
	@Autowired
	private AppUserMapper appUserMapper;

	@SneakyThrows(value = BDException.class)
	@Override
	public int addFxLevelRecord(String parantOpenId, String childOpenId) {
		FxLevelRecord fxLevelRecord = new FxLevelRecord();
		fxLevelRecord.setParentOpenId(parantOpenId);
		fxLevelRecord.setChildOpenId(childOpenId);
		return fxLevelRecordMapper.insert(fxLevelRecord);
	}
	
	@SneakyThrows(value = BDException.class)
	@Override
	public List<AppUser> querySuperior(String myOpenId, Integer level) {
		return queryLevelTemplate("childOpenId", level, myOpenId);
	}

	@SneakyThrows(value = BDException.class)
	@Override
	public List<AppUser> queryLowerLevel(String myOpenId, Integer level) {
		return queryLevelTemplate("parentOpenId", level, myOpenId);
	}

	@SneakyThrows(value = BDException.class)
	@Override
	public List<Map.Entry<String, List<AppUser>>> querySuperiorGroup(String myOpenId, Integer level) {
		return queryLevelGroupTemplate("childOpenId", level, myOpenId);
	}

	@SneakyThrows(value = BDException.class)
	@Override
	public List<Map.Entry<String, List<AppUser>>> queryLowerLevelGroup(String myOpenId, Integer level) {
		return queryLevelGroupTemplate("parentOpenId", level, myOpenId);
	}

	@Override
	public List<AppUser> queryLevelTemplate(String templateName, Integer level, String myOpenId) {
		// 获取默认级别
		level = getDefaultLevel(level);

		List<AppUser> userList = Lists.newArrayList();

		List<String> openIds = Arrays.asList(myOpenId);

		for (int i = 0; i < level; i++) {

			if (CollectionUtils.isNotEmpty(openIds)) {
				EntityWrapper<FxLevelRecord> entityWrapper = new EntityWrapper<FxLevelRecord>();
				entityWrapper.in(templateName, openIds);
				List<FxLevelRecord> selectList = fxLevelRecordMapper.selectList(entityWrapper);
                //根据openid查询用户信息没有做多表查询
				List<AppUser> userLists = appUserMapper.selectList(new EntityWrapper<AppUser>().in("openId", openIds));
				if(org.apache.commons.collections.CollectionUtils.isNotEmpty(userLists)) {
					userList.addAll(userLists);
				}else
					
				openIds = Lists.newArrayList();

				if (StringUtils.equals("parentOpenId", templateName)) {
					openIds = selectList.parallelStream().filter(f -> StringUtils.isNotBlank(f.getChildOpenId()))
							.map(FxLevelRecord::getChildOpenId).collect(Collectors.toList());
				} else if (StringUtils.equals("childOpenId", templateName)) {
					openIds = selectList.parallelStream().filter(f -> StringUtils.isNotBlank(f.getParentOpenId()))
							.map(FxLevelRecord::getParentOpenId).collect(Collectors.toList());
				}
			}
		}

		return userList;
	}

	@Override
	public List<Map.Entry<String, List<AppUser>>> queryLevelGroupTemplate(String templateName, Integer level,
			String myOpenId) {
		// 获取默认级别
		level = getDefaultLevel(level);

		Map<String, List<AppUser>> group = Maps.newLinkedHashMap();

		List<String> openIds = Arrays.asList(myOpenId);

		for (int i = 0; i < level; i++) {
			EntityWrapper<FxLevelRecord> entityWrapper = new EntityWrapper<FxLevelRecord>();
			entityWrapper.in(templateName, openIds);
			List<FxLevelRecord> selectList = fxLevelRecordMapper.selectList(entityWrapper);
			int levelInt = i + 1;
			
			 //根据openid查询用户信息没有做多表查询
			List<AppUser> userLists = appUserMapper.selectList(new EntityWrapper<AppUser>().in("openId", openIds));
			if(org.apache.commons.collections.CollectionUtils.isNotEmpty(userLists)) {
				group.put("第" + levelInt + "级", userLists);
			}

			openIds = Lists.newArrayList();

			if (StringUtils.equals("parentOpenId", templateName)) {
				openIds = selectList.parallelStream().filter(f -> StringUtils.isNotBlank(f.getChildOpenId()))
						.map(FxLevelRecord::getChildOpenId).collect(Collectors.toList());
			} else if (StringUtils.equals("childOpenId", templateName)) {
				openIds = selectList.parallelStream().filter(f -> StringUtils.isNotBlank(f.getParentOpenId()))
						.map(FxLevelRecord::getParentOpenId).collect(Collectors.toList());
			}
		}
		if (MapUtils.isNotEmpty(group)) {
			List<Map.Entry<String, List<AppUser>>> list = new ArrayList<Map.Entry<String, List<AppUser>>>(
					group.entrySet());
			return list;
		}
		return Lists.newArrayList();
	}
}
